package fr.afcepf.atod.gestionVP.dao;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import fr.afcepf.atod.gestionVP.data.Employe;


@Component("DaoEmployeImpl")  //pour une prise en charge par Spring
@Transactional
public class DaoEmployeImpl implements IDaoEmploye {

	//@PersistenceContext (d'origine Ejb3)
	//permet d'initialiser entityManager 
	//en fonction de META-INF/persitence.xml 
	@PersistenceContext(unitName="Gestion Vente Paiement")
	private EntityManager em;

//****************************************************************************************
	//Methode provenant de IDao
//****************************************************************************************

	public void createEmploye(Employe nf) {
		em.persist(nf);

	}

	public void deleteEmploye(int id) {
		em.remove(this.getEmployeById(id));

	}

	public void updateEmploye(Employe nf) {
		em.merge(nf);

	}

	@SuppressWarnings("unchecked")
	public Collection<Employe> getAllEmploye() {
		Query query = em.createQuery("SELECT em.nom FROM Employe as em");	
		return query.getResultList();
	}


	public Employe getEmployeById(int id) {
			return em.find(Employe.class, id);
	}
	

}
